package com.thfw.test.view.mq;

import android.content.Context;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import org.eclipse.paho.client.mqttv3.MqttConnectOptions;

import java.util.UUID;

/**
 * Created by scooper_lxx on 2017/2/16.
 */

public class ConfigBean {

    private int qos;
    private boolean isretained;
    private String apolloUserName;
    private String apolloUserPassword;
    private String apolloUrl;
    private String apolloId;
    private Context context;

    private String[] topic;

    private void initConfigBean() {
        this.qos = 1;
        this.isretained = false;
        this.apolloUserName = "test";
        this.apolloUserPassword = "test";
    }

    public ConfigBean(Context context) {
        this.context = context;
        this.initConfigBean();
    }

    public ConfigBean(Context context, String apolloUrl) {
        this.context = context;
        this.initConfigBean();
        this.apolloUrl = TextUtils.isEmpty(apolloUrl) ? "tcp://127.0.0.1:61613" : apolloUrl;
    }

    public ConfigBean(Context context, String apolloUserName, String apolloUserPassword, String apolloUrl) {
        this.context = context;
        this.apolloUserName = TextUtils.isEmpty(apolloUserName) ? "admin" : apolloUserName;
        this.apolloUserPassword = TextUtils.isEmpty(apolloUserPassword) ? "password" : apolloUserPassword;
        this.apolloUrl = TextUtils.isEmpty(apolloUrl) ? "tcp://127.0.0.1:61613" : apolloUrl;
    }

    public ConfigBean(Context context, String apolloUserName, String apolloUserPassword, String apolloUrl, int qos, boolean isretained) {
        this.context = context;
        this.apolloUserName = TextUtils.isEmpty(apolloUserName) ? "admin" : apolloUserName;
        this.apolloUserPassword = TextUtils.isEmpty(apolloUserPassword) ? "password" : apolloUserPassword;
        this.apolloUrl = TextUtils.isEmpty(apolloUrl) ? "tcp://127.0.0.1:61613" : apolloUrl;
        this.qos = qos;
        this.isretained = isretained;
    }

    public int getQos() {
        return this.qos;
    }

    public void setQos(int qos) {
        this.qos = qos;
    }

    public boolean getIsretained() {
        return this.isretained;
    }

    public void setIsretained(boolean isretained) {
        this.isretained = isretained;
    }

    public String getApolloUserName() {
        return this.apolloUserName;
    }

    public void setApolloUserName(String apolloUserName) {
        this.apolloUserName = apolloUserName;
    }

    public String getApolloUserPassword() {
        return this.apolloUserPassword;
    }

    public void setApolloUserPassword(String apolloUserPassword) {
        this.apolloUserPassword = apolloUserPassword;
    }

    public String getApolloUrl() {
        return this.apolloUrl;
    }

    public Context getContext() {
        return context;
    }

    public void setApolloUrl(String apolloUrl) {
        this.apolloUrl = apolloUrl;
    }

    public String getClientId() {
        UUID deviceUuid;
        try {
            TelephonyManager tm = (TelephonyManager) this.context.getSystemService(Context.TELEPHONY_SERVICE);
            String ANDROID_ID = Settings.System.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);

            deviceUuid = new UUID(hashCode(), ((long) tm.getDeviceId().hashCode() << 32) | ANDROID_ID.hashCode());
        } catch (Exception e) {
            deviceUuid = UUID.randomUUID();
        }
        String id = deviceUuid.toString();
        return apolloId + id;
    }

    public MqttConnectOptions buildOptions() {
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();

        String username = TextUtils.isEmpty(this.getApolloUserName()) ? "admin" : this.getApolloUserName();
        String password = TextUtils.isEmpty(this.getApolloUserName()) ? "password" : this.getApolloUserPassword();

        mqttConnectOptions.setUserName(username);
        mqttConnectOptions.setPassword(password.toCharArray());

        mqttConnectOptions.setConnectionTimeout(10);
/*        mqttConnectOptions.setKeepAliveInterval(20);*/
        //不能修改
        mqttConnectOptions.setCleanSession(true);

        return mqttConnectOptions;
    }

    public String getApolloId() {
        return apolloId;
    }

    public void setApolloId(String apolloId) {
        this.apolloId = apolloId;
    }

    public String[] getTopic() {
        return topic;
    }

    public void setTopic(String[] topic) {
        this.topic = topic;
    }
}
